package com.maimao.kill.controller;

import com.maimao.core.model.resp.Result;
import com.maimao.core.utils.DateUtils;
import com.maimao.model.kill.KillGoods;
import com.maimao.redis.RedisLock;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;

/**
 * @author MaoLin Wang
 * @date 2021/5/20 下午5:26
 */
@RestController
@RequestMapping("/kill/test")
public class TestController {

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/testlock")
    public void testlock(@RequestParam("id")Integer sleep) {
        System.out.println("500个线程开始时间:" + DateUtils.now());
        for (int i = 0; i < 500; i++) {
            RedisLock redisLock = new RedisLock(redisTemplate, "testLock", 2,2);
            try {
                if (redisLock.lock()) {
                    System.out.println("获取到锁");
                    redisTemplate.opsForValue().increment("test_aa");
                }
            } finally {
                redisLock.unlock();
            }
        }
        System.out.println("500个线程结束时间:" + DateUtils.now());

    }

    @GetMapping("/testlocktimer")
    public void testlocktimer() {
//        System.out.println("100个线程开始时间:" + DateUtils.now());
        for (int i = 0; i < 100; i++) {
            RedisLock redisLock = new RedisLock(redisTemplate, "testLock",2);
            Timer timer = null;
            try {
                if (redisLock.tryLock()) {
                    timer = continueLock("testLock");
                    System.out.println("获取到锁");
                    redisTemplate.opsForValue().increment("test_aa");
                    try {
                        Thread.sleep(0L);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            } finally {
                if (timer != null) {
                    timer.cancel();
                }
                redisLock.unlock();
            }
        }
//        System.out.println("100个线程结束时间:" + DateUtils.now());

    }

    @Autowired
    private RedissonClient redissonClient;

    @GetMapping("/redisson")
    public void redisson(@RequestParam("sleep")Integer sleep) {
        RLock lock = redissonClient.getLock("testLock");
        System.out.println("500个线程开始时间:" + DateUtils.now());
        for (int i = 0; i < 500; i++) {
            try {
                if (lock.tryLock(2, TimeUnit.SECONDS)) {
                    System.out.println("获取到锁");
                    redisTemplate.opsForValue().increment("test_aa");
                    try {
                        if (sleep==1){
                            Thread.sleep(100L);
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {

                lock.unlock();
            }
        }
        System.out.println("500个线程结束时间:" + DateUtils.now());

    }

    private Timer continueLock(String lockKey) {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                redisTemplate.expire(lockKey, 60, TimeUnit.SECONDS);
            }
        }, 0, 1);
        return timer;
    }
}
